﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace bts_webapp
{
    public partial class Search : BasePage
    {
        public void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem) {
                var dataItem = e.Item as GridDataItem;
                dataItem["Name"].Text = string.Format("<a href='#' onclick=\"openWin('ObjectInfo.aspx?type={1}&id={2}');\">{0}</a>",
                                            dataItem["Name"].Text, GetObjectType(dataItem["Type"].Text), dataItem["Id"].Text);
                dataItem["Type"].Text = GetObjectType2(dataItem["Type"].Text);
            }
                                                    
        }

        private string GetObjectType2(string type)
        {
            switch (type)
            {
                case "Station":
                    return "Trạm";
                case "Device":
                    return "Thiết bị";
                case "Flow":
                    return "Luồng";
                default:
                    return string.Empty;
            }
        }

        private string GetObjectType(string type) {
            switch (type) { 
                case "Station":
                    return "st";
                case "Device":
                    return "dv";
                case "Flow":
                    return "fl";
                default:
                    return string.Empty;
            }
        }

        public void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.RadGrid1.DataSource = GetTable();
                return;
            }

            SearchType type = SearchType.None;
            if (this.chkDevice.Checked)
                type |= SearchType.Device;
            if (this.chkStation.Checked)
                type |= SearchType.Station;
            if (this.chkFlow.Checked)
                type |= SearchType.Flow;

            string textSearch = this.txtSearchContent.Text.Trim();

            var table = GetData(type, textSearch);

            this.RadGrid1.DataSource = table;
        }

        [Flags]
        public enum SearchType
        { 
            None = 0,
            Station = 1,
            Device = 2,
            Flow = 4,
            All = 8
        }
        
        public void btnSubmit_Click(object sender, EventArgs e)
        {
            this.RadGrid1.Rebind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.chkDevice.Checked = this.chkFlow.Checked = this.chkStation.Checked = true;
            }
        }

        private DataTable GetTable() {
            var table = new DataTable("Search");
            table.Columns.Add("Id", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Type", typeof(string));

            return table;
        }

        private DataTable GetData(SearchType type, string searchText) 
        {
            var table = GetTable();

            if ((type & SearchType.Station) == SearchType.Station) {
                var list = DbContext.VIBA_TRAMs.Where(t => t.Tram_Ten.ToLower().Contains(searchText.ToLower())).ToList();
                if (list != null && list.Count > 0) {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var row = table.NewRow();
                        row.ItemArray = new object[] { list[i].Tram_Id, list[i].Tram_Ten, "Station" };
                        table.Rows.Add(row);
                    }
                }
            }

            if ((type & SearchType.Device) == SearchType.Device)
            {
                var list = DbContext.VIBA_THIET_BIs.Where(t => t.ThietBi_Ten.ToLower().Contains(searchText.ToLower())).ToList();
                if (list != null && list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var row = table.NewRow();
                        row.ItemArray = new object[] { list[i].ThietBi_Id, list[i].ThietBi_Ten, "Device" };
                        table.Rows.Add(row);
                    }
                }
            }

            if ((type & SearchType.Flow) == SearchType.Flow)
            {
                var list = DbContext.VIBA_LUONGs.Where(t => t.Luong_Ten.ToLower().Contains(searchText.ToLower())).ToList();
                if (list != null && list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var row = table.NewRow();
                        row.ItemArray = new object[] { list[i].Luong_Id  , list[i].Luong_Ten, "Flow" };
                        table.Rows.Add(row);
                    }
                }
            }


            return table;
        }
    }
}